<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    'use strict';
    //判断this指向:

    //1. 强制绑定:如果函数是被call\apply\bind调用,则指向他们的第一个参数
    function fn1() {
      console.log(this)
    }

    fn1.call({}); //fn1的this指向对象{}
    fn1.call(null); //fn1的this指向对象window
    fn1.call(undefined); //fn1的this指向对象window
    fn1.call(1); //fn1的this指向对象 Object(1)
    fn1.call("abc"); //fn1的this指向对象{} Object("abc")


    //2. 函数是否被new调用,如果被new调用则指向实例化对象
    function fn2() {
      console.log(this)
    }
    new fn2(); //实例化对象
    new fn2; //实例化对象

    //3. 函数是否是被上下文对象调用的
    const obj = {
      fn() {
        console.log(this);
      }
    }
    obj.fn() //this指向obj

    const ex = obj.fn; //(隐式丢失,fn的this指向要看ex的调用方式)
    ex() //window

    //4. 默认调用(严格模式下指向undefined 非严格模式下指向window)
    function fn4() {
      console.log(this, "????");
    }
    fn4(); //window


    //5.直接在js环境中打印this
    console.log(this) //window
  </script>
</body>

</html>